// import { fileURLToPath, URL } from 'node:url'

import { resolve } from "path"

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'

// 按需引入 UI 组件
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

import { createSvgIconsPlugin } from "vite-plugin-svg-icons"

import { name, version, dependencies, devDependencies } from "./package.json";

// 平台的名称、版本、运行所需的 node 版本、依赖、构建时间的类型提示
const __APP_INFO__ = {
  pkg: { name, version, dependencies, devDependencies },
  buildTimestamp: Date.now(),
};

const pathSrc = resolve(__dirname, "src");
// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
    AutoImport({
      // 自动导入 Vue 相关函数，如：ref, vue-router等
      imports: ['vue', 'vue-router'],
      eslintrc: {
        enabled: false, // 若没此json文件，第一次先开启，生成后记得关闭
      },
      resolvers: [ElementPlusResolver()]
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    }),
    createSvgIconsPlugin({
      // 缓存图标位置
      iconDirs: [resolve(pathSrc, "assets/icons")],
      symbolId: "icon-[dir]-[name]",
    }),
  ],
  // 预加载项目必需的组件
  optimizeDeps: {
    include: [
      "vue",
      "vue-router",
      "element-plus",
      "pinia",
      "axios",
      "@vueuse/core",
      "sortablejs",
      "exceljs",
      "path-to-regexp",
      "echarts",
      "@wangeditor/editor",
      "@wangeditor/editor-for-vue",
      "vue-i18n",
      "path-browserify",
      "element-plus/es/components/form/style/css",
      "element-plus/es/components/form-item/style/css",
      "element-plus/es/components/button/style/css",
      "element-plus/es/components/input/style/css",
      "element-plus/es/components/input-number/style/css",
      "element-plus/es/components/switch/style/css",
      "element-plus/es/components/upload/style/css",
      "element-plus/es/components/menu/style/css",
      "element-plus/es/components/col/style/css",
      "element-plus/es/components/icon/style/css",
      "element-plus/es/components/row/style/css",
      "element-plus/es/components/tag/style/css",
      "element-plus/es/components/dialog/style/css",
      "element-plus/es/components/loading/style/css",
      "element-plus/es/components/radio/style/css",
      "element-plus/es/components/radio-group/style/css",
      "element-plus/es/components/popover/style/css",
      "element-plus/es/components/scrollbar/style/css",
      "element-plus/es/components/tooltip/style/css",
      "element-plus/es/components/dropdown/style/css",
      "element-plus/es/components/dropdown-menu/style/css",
      "element-plus/es/components/dropdown-item/style/css",
      "element-plus/es/components/sub-menu/style/css",
      "element-plus/es/components/menu-item/style/css",
      "element-plus/es/components/divider/style/css",
      "element-plus/es/components/card/style/css",
      "element-plus/es/components/link/style/css",
      "element-plus/es/components/breadcrumb/style/css",
      "element-plus/es/components/breadcrumb-item/style/css",
      "element-plus/es/components/table/style/css",
      "element-plus/es/components/tree-select/style/css",
      "element-plus/es/components/table-column/style/css",
      "element-plus/es/components/select/style/css",
      "element-plus/es/components/option/style/css",
      "element-plus/es/components/pagination/style/css",
      "element-plus/es/components/tree/style/css",
      "element-plus/es/components/alert/style/css",
      "element-plus/es/components/radio-button/style/css",
      "element-plus/es/components/checkbox-group/style/css",
      "element-plus/es/components/checkbox/style/css",
      "element-plus/es/components/tabs/style/css",
      "element-plus/es/components/tab-pane/style/css",
      "element-plus/es/components/rate/style/css",
      "element-plus/es/components/date-picker/style/css",
      "element-plus/es/components/notification/style/css",
      "element-plus/es/components/image/style/css",
      "element-plus/es/components/statistic/style/css",
      "element-plus/es/components/watermark/style/css",
      "element-plus/es/components/config-provider/style/css",
      "element-plus/es/components/text/style/css",
      "element-plus/es/components/drawer/style/css",
      "element-plus/es/components/color-picker/style/css",
      "element-plus/es/components/backtop/style/css",
      "element-plus/es/components/message-box/style/css",
      "element-plus/es/components/skeleton/style/css",
      "element-plus/es/components/skeleton/style/css",
      "element-plus/es/components/skeleton-item/style/css",
      "element-plus/es/components/badge/style/css",
      "element-plus/es/components/steps/style/css",
      "element-plus/es/components/step/style/css",
      "element-plus/es/components/avatar/style/css",
      "element-plus/es/components/descriptions/style/css",
      "element-plus/es/components/descriptions-item/style/css",
      "element-plus/es/components/checkbox-group/style/css",
      "element-plus/es/components/progress/style/css",
      "element-plus/es/components/image-viewer/style/css",
      "element-plus/es/components/empty/style/css",
    ],
  },
  base: '/', // 开发或生产环境服务的公共基础路径
  server: {
    // 设置 https 代理
    proxy: {
      '/bio_platform': {
        target: 'http://192.168.20.51:9277',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/bio_platform/, '')
      }
    }
  },
  resolve: {
    alias: {
      '@': pathSrc
    },
  },
  css: {
    preprocessorOptions: {
      // 定义全局 SCSS 变量
      scss: {
        // 定义全局样式变量等
        api: "modern-compiler",
        additionalData: `@use "@/assets/styles/variables.scss" as *;`
      },
    },
  },
  define: {
    __APP_INFO__: JSON.stringify(__APP_INFO__),
  },
})
